/**
 * 常量
 */
// 密码字符集
const dataObj = {
    A: [...'0123456789'],
    B: [...'abcdefghijklmnopqrstuvwxyz'],
    C: [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'],
    D: [...'.@$%&()_-+?']
};

/**
 * dom 获取
 */
// 选择按钮
var clickBut = document.getElementsByTagName('input');
// 结果输出
var resultText = document.getElementById('result-text');
// 历史记录
var createdHistory = document.getElementById('createdHistory');
// 弹窗
var dialog = document.getElementById('dialog');
// 弹窗内容
var showContent = document.getElementById('showContent');
// 弹窗确认按钮
var tipsConfirm = document.getElementById('tipsConfirm');

/**
 * 工具方法
 */
// 随机数字
function randomNum(min, max) {
    return ~~(Math.random() * max) + min;
}
// 随机字符串
function randomString(list, len) {
    var result = '';
    for (var i = 0; i < len; i++) {
        result += list[randomNum(0, list.length)]
    }
    return result;
}
// 复制文字到剪切板
function copyText(text) {
    if (!text) return;
    // 复制借助input实现
    document.execCommand("Copy");
    var inputZ = document.createElement('input');
    inputZ.setAttribute('id', 'inputCopy');
    inputZ.value = text;
    document.body.appendChild(inputZ);
    document.getElementById('inputCopy').select();
    document.execCommand('Copy')
    document.body.removeChild(inputZ);
}

/**
 * 功能实现
 */
// 显示弹窗
function showDialog(content) {
    dialog.style.display = 'flex'
    showContent.innerHTML = content
}
// 关闭弹窗
function closeDialog() {
    dialog.style.display = 'none'
}
// 创建密码
function createPassword() {
    var selectfrom = document.selectfrom.elements
    var gradeform = document.gradeform.elements
    var len = selectfrom.length
    var num = "0";
    for (var i = 0; i < len; i++) {
        if (selectfrom[i].checked) {
            num = selectfrom[i].value
        }
    }
    if (num == 0) {
        if (!/^\d+$/.test(selectfrom[len - 1].value)) {
            showDialog('请输入正确的位数')
            selectfrom[len - 1].value = '32'
            return
        }
        num = selectfrom[len - 1].value
    }
    var list = [];
    ;[].forEach.call(gradeform, (item) => {
        if (item.checked) {
            list.push(...dataObj[item.value])
        }
    });
    if (!list.length) {
        return showDialog('至少选中一种类型')
    }
    var content = randomString(list, +num)
    resultText.innerText = content
    resultText.classList.remove('copy-text')
    addHistory(content)
}
// 添加历史
function addHistory(content) {
    var li = document.createElement('li');
    li.classList.add('history-item')
    li.innerText = content
    li.onclick = function () {
        copyText(content)
        showDialog('已复制内容到剪贴板')
    }
    createdHistory.appendChild(li)
}
// 复制输出结果
function copyResult() {
    var text = resultText.innerText.trim();
    if (!text) return;
    copyText(text)
    showDialog('已复制内容到剪贴板')
    resultText.classList.add('copy-text')
}
// 添加点击事件
window.onload = createPassword
for (let i = 0; i < clickBut.length; i++) {
    let item = clickBut[i]
    if (item.type === 'radio' || item.type == 'checkbox') {
        item.onclick = createPassword
    }
}
tipsConfirm.onclick = closeDialog